package com.system.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.business.domain.DailyTrain;
import com.system.business.domain.Train;
import com.system.business.req.TrainQueryReq;
import com.system.business.req.TrainSaveReq;
import com.system.business.resp.DailyTrainQueryResp;
import com.system.business.resp.TrainQueryResp;
import com.system.business.service.TrainService;
import com.system.business.mapper.TrainMapper;
import com.system.common.resp.PageResp;
import com.system.common.utils.SnowUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Admin
* @description 针对表【train(车次)】的数据库操作Service实现
* @createDate 2024-03-21 15:02:15
*/
@Service
public class TrainServiceImpl extends ServiceImpl<TrainMapper, Train>
    implements TrainService{

    @Autowired
    private TrainMapper trainMapper;

    @Override
    public PageResp<TrainQueryResp> queryList(TrainQueryReq req) {
        Page<Train> pageCondition = new Page<>(req.getPage(), req.getSize());
        QueryWrapper<Train> passengerQueryWrapper = new QueryWrapper<>();
        Page<Train> pageData = trainMapper.selectPage(pageCondition,passengerQueryWrapper);
        List<TrainQueryResp> list = BeanUtil.copyToList(pageData.getRecords(), TrainQueryResp.class);

        PageResp<TrainQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageData.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    @Override
    public void save(TrainSaveReq req) {
        DateTime now = DateTime.now();

        Train train = BeanUtil.copyProperties(req, Train.class);
        if (ObjectUtil.isNull(train.getId())) {
            train.setId(SnowUtil.getSnowflakeNextId());
            train.setCreateTime(now);
            train.setUpdateTime(now);
            trainMapper.insert(train);
        } else {
            train.setUpdateTime(now);
            trainMapper.updateById(train);
        }
    }
}




